  (function ($) {
	$.fn.fadeTransition = function(options) {
	  var options = $.extend({pauseTime: 500, transitionTime: 500}, options);
	  var transitionObject;

	  Trans = function(obj) {
		var timer = null;
		var current = 0;
		var els = $("> *", obj).css("display", "none").css("left", "0").css("top", "0").css("position", "relative");
		$(obj).css("position", "relative");
		$(els[current]).css("display", "block");

		function transition(next) {
		  $(els[current]).fadeOut(options.transitionTime);
		  $(els[next]).fadeIn(options.transitionTime);
		  current = next;
		 // cue();
		};

		function cue() {
		  if ($("> *", obj).length < 2) return false;
		  if (timer) clearTimeout(timer);
		  timer = setTimeout(function() { transition((current + 1) % els.length | 0)} , options.pauseTime);
		};
		
		this.showItem = function(item) {
		  if (timer) clearTimeout(timer);
		  transition(item);
		};

		cue();
	  }

	  this.showItem = function(item) {
		transitionObject.showItem(item);
	  };

	  return this.each(function() {
			transitionObject = new Trans(this);
	  });
	}

  })(jQuery);

  var page_transition = {
	tr: null,
	totalItem:0,
	currentItem:0,
	pre_btn:null,
	next_btn:null,
	init: function() {
	  page_transition.tr = $(".sarea").fadeTransition({pauseTime: 700000000, transitionTime: 500});
	  $("ul.navicontroller a").each(function(idx) {
	  		page_transition.totalItem++;
			$(this).click(function() {
				page_transition.tr.showItem(idx); 
				page_transition.currentItem = idx;
				page_transition.update_image_description();
				return false;
				}
			)
	  });
	  page_transition.addctrbtn();
	},
	addctrbtn:function(){
		//add prev button
		page_transition.pre_btn = $("<div class='bx-prev'><a href='#' onclick='javascript:return false;'></a></div>");
		
		 $(".sarea").parent().append(page_transition.pre_btn);
		 page_transition.pre_btn.bind('click',function(){
		 	if(page_transition.currentItem == 0)
		 		page_transition.currentItem = page_transition.totalItem;
		 	page_transition.currentItem--;
		 	page_transition.tr.showItem(page_transition.currentItem); 
		 	page_transition.update_image_description();
		 });
		 //add next button
		 
		 page_transition.next_btn = $("<div class='bx-next'><a href='#' onclick='javascript:return false;'></a></div>");
		 $(".sarea").parent().append(page_transition.next_btn);
		 page_transition.next_btn.bind('click',function(){
		 	if(page_transition.currentItem == (page_transition.totalItem -1))
		 		page_transition.currentItem = -1;
		 	page_transition.currentItem++;
		 	page_transition.tr.showItem(page_transition.currentItem); 
		 	page_transition.update_image_description();
		 });
		 
		 page_transition.locationbtn();
	},
	locationbtn:function(){
		var position = $(".sarea").position();
		page_transition.pre_btn.css("left",position.left - 0);
		 page_transition.pre_btn.css("top",position.top + $(".sarea").height()/2);
		  page_transition.next_btn.css("left",position.left + $(".sarea").width() -30);
		 page_transition.next_btn.css("top",position.top + $(".sarea").height()/2);
		 
	},
	update_image_description:function(){
		//update image description
	 	$('#contactf-img2-normal').text(global_image_description_table[page_transition.currentItem]);
	},
	show: function(idx) {
	  if (page_transition.tr.timer) clearTimeout(page_transition.tr.timer);
	  page_transition.tr.showItem(idx);
	  
	}
  };
var page_transition_full = {
	tr: null,
	totalItem:0,
	currentItem:0,
	pre_btn:null,
	next_btn:null,
	init: function() {
	  page_transition_full.tr = $(".sareafull").fadeTransition({pauseTime: 700000000, transitionTime: 500});
	  $("ul.navicontrollerfull a").each(function(idx) {
			  page_transition_full.totalItem++;
			$(this).click(function() {
				page_transition_full.tr.showItem(idx); 
				page_transition_full.currentItem = idx;
				page_transition_full.update_image_description();
				return false;
				}
			)
	  });
	 // page_transition_full.addctrbtn();
	},
	addctrbtn:function(){
		
		//add prev button
		page_transition_full.pre_btn = $("<div class='bx-prev'><a href='#' onclick='javascript:return false;'></a></div>"); 
		 
		 $(".sareafull").parent().append(page_transition_full.pre_btn);
		 page_transition_full.pre_btn.bind('click',function(){
		 	if(page_transition_full.currentItem == 0)
		 		page_transition_full.currentItem = page_transition_full.totalItem;
		 	page_transition_full.currentItem--;
		 	page_transition_full.tr.showItem(page_transition_full.currentItem); 
		 	page_transition_full.update_image_description();
		 });
		 //add next button
		 
		 page_transition_full.next_btn = $("<div class='bx-next'><a href='#' onclick='javascript:return false;'></a></div>");
		 
		 $(".sareafull").parent().append(page_transition_full.next_btn);
		  page_transition_full.next_btn.bind('click',function(){
		 	if(page_transition_full.currentItem == (page_transition_full.totalItem -1))
		 		page_transition_full.currentItem = -1;
		 	page_transition_full.currentItem++;
		 	page_transition_full.tr.showItem(page_transition_full.currentItem); 
		 	page_transition_full.update_image_description();
		 });
		 page_transition_full.locationbtn();
	},
	locationbtn:function(){
		var position = $(".sareafull").position();
		page_transition_full.pre_btn.css("left",position.left - 0);
		 page_transition_full.pre_btn.css("top",position.top + $(".sareafull").height()/2);
		 page_transition_full.next_btn.css("left",position.left + $(".sareafull").width() -30);
		 page_transition_full.next_btn.css("top",position.top + $(".sareafull").height()/2);
	},
	update_image_description:function(){
		//update image description
	 	$('#contactf-img2-full').text(global_image_description_table[page_transition_full.currentItem]);
	},
	show: function(idx) {
	  if (page_transition_full.tr.timer) clearTimeout(page_transition_full.tr.timer);
	  page_transition_full.tr.showItem(idx);
	}
  };
  
  $(function(){
  	page_transition.init();
  	page_transition_full.init();
  	$(window).resize(function() {
  			page_transition.locationbtn();
  			page_transition_full.locationbtn();
	});
  });
  